﻿; This file contains the information for showing the issue list.
; Functions:
;	show_issue_list(language) -- Creates the window
;	list_issues(status,type) -- grab/list issues

; This function is only called once per launch
show_issue_list()
{

; Get global variables
global language
global issue_win_title
global new_issue_win_title
global issue_listview
global user_prefs

; Grab language strings
; Ex: get_language_string("types",language) gets us the string for "type" in our current language

; Top buttons
newissue := get_language_string("newissue",language)
refreshissuelist := get_language_string("refreshissuelist",language)
options := get_language_string("options",language)

; Dropdownlist/Filter row
; Status(es)
statuses := get_language_string("open",language) . "||" . get_language_string("closed",language) . "|" . get_language_string("all",language)
status := get_language_string("status",language)
;open := 
;closed := 
all := get_language_string("all",language)

;statuses = %open%||%closed%|%all%

; Type(s)
type := get_language_string("type",language)
types := get_language_string("types",language)


; Typelist is a CSV, so we'll loop through values to build our filter list
typelist = %all%||
num_types = 1
Loop,parse, types,`,
{
	; If it's not a blank value
	If (A_Loopfield != "") 
	{
		num_types += 1 ; Increment the counter that sets the dropdownlist height (number of options shown without scrolling)
		typelist=%typelist%%A_loopfield%|  ; Add it to our list
	}
}

; Listview column headers (that we haven't retrieved yet)
ID := get_language_string("ID",language)
date := get_language_string("date",language)
subject := get_language_string("subject",language)
priority := get_language_string("priority",language)

; Listview Color
issue_LVcolor := ini_getvalue(user_prefs,"listviewcolors","issues")

; Generate Gui
; Generated using SmartGUI Creator 4.0

; Buttons on top
Gui, add, button, x12 y10 w120 h40 gnew_issue, %newissue%
Gui, add, button, x142 y10 w120 h40 grefresh_list_view, %refreshissuelist%
Gui, add, button, x410 y10 w120 h40, %options%

; Text above LV
Gui, Add, Text, x12 y63 w60 h20 , %status%:
Gui, Add, Text, x182 y63 w70 h20 , %type%:

; Drop down lists above LV
Gui, Add, DropDownList, x72 y60 w90 h21 r3 gdropdownlist, %statuses%
Gui, Add, DropDownList, x252 y60 w100 h21 r%num_types% gdropdownlist, %typelist%

; List View
Gui, Add, ListView, x12 y90 w520 h210 vissue_listview gissue_listview_response, %ID%|%date%|%type%|%priority%|%status%|%subject%

; Show the GUI
Gui, Show, h315 w550, %issue_win_title%
; msgbox,%issue_listview%

; Customize the label to be run when the window is closed 
Gui 1:+LabelIssueList +Resize

; No options button until it's finished
GuiControl,disable,button3

; List issues
refresh_list_view:
dropdownlist:

;starttime := A_TickCount
;Loop, 1000 ; performance testing
	list_issues() ; Call listing function

WinActivate,%issue_win_title%
; Performance tracking
;elapsedtime := A_tickcount - starttime
; msgbox, %elapsedtime%ms

; Listview coloring - disabled until I can make it work more reliably
; LVA_ListViewAdd("issue_listview","AR RB0x"issue_LVcolor)

Return

; Label for new issue button
new_issue:
new_issue_gui()
WinWaitClose,%new_issue_win_title%
Goto, refresh_list_view ; Refresh list after a new issue window has been closed
Return

; Reaction to user input
issue_listview_response:
If (A_GuiEvent = "DoubleClick")
{
	
	LV_GetText(issuenumber,A_EventInfo)
	view_issue(issuenumber)
	; ; msgbox, Issue #%issuenumber% doubleclicked!
}
Return

; Resizing code
IssueListSize:
ControlMove,syslistview321,,, A_GuiWidth - 25, A_GuiHeight - 105,%issue_win_title%
LV_ModifyCol(6,"AutoHDR") ; Resize last column
Return

; Issue List Close
IssueListClose:
;; msgbox, Closing!
ExitApp
Return

}

;[listviewcolors]
;issues=F0F8FF
;comments=F0F8FF


; This function performs the searching/listing of issues based on selected filters
; We pass the window title so it can grab the DDL filters from the list window (and allow 
list_issues()
{

Gui, 1: Default

; Access issue list + language strings
global issues
global language
global user_prefs
global issue_win_title
select_all := get_language_string("all",language)

; Hide the listview
Control,Hide,,Syslistview321,%issue_win_title%

; Clear it
LV_Delete()  ; Clear all rows from the ListView.

; Get a list of the selected items
ControlGet,status,Choice,,Combobox1,%issue_win_title%
ControlGet,filter_type,Choice,,Combobox2,%issue_win_title%

; Be dynamic with open/closed language
open := get_language_string("open",language)
closed := get_language_string("closed",language)

; Check what we're looking for
If (InStr(status,open))
	status=open
Else If (InStr(status,closed))
	status=closed

; Select by status (open or closed)
If (status != select_all)
	issue_list := ini_getvalue(issues,"issues",status)
Else ; If we want to list all of them, we'll need to make a list
{
	num_issues := ini_getvalue(issues,"issues","number") ; Get the number of issues
	issue_list= ; Set an empty var
	Loop,%num_issues%
		issue_list=%issue_list%%A_Index%, ; Build a CSV list of them
}

; Filter by type (if needed)
If (filter_type != select_all)
{
	issueID_list = %issue_list% ; Set variables to rebuild filtered list
	issue_list=
	Loop,parse,issueID_list,`,
	{
		If (A_loopfield != "") ; Avoid blank issue IDs
		{
			; Get the types of issues from the list
			current_issue_type := ini_getvalue(issues,"issue"A_Index,"type")
			If (current_issue_type = filter_type)
				issue_list=%issue_list%%A_Index%,
		}
	}
}

; Add data to the listview (based on the ID list determined above)
If (issue_list != "")
{
	Loop,parse,issue_list,`,
	{
		If (A_loopfield != "") ; Avoid blank issue IDs
		{
			; Load issue data (based on ID)
			; We load a section and then break it apart with a nested parsing loop
			section := ini_getsection(issues,"issue"A_loopfield)

			; Read one line at a time
			Loop,parse,section, `n, `r
			{
				line_name=
				line_contents=
				If A_Index > 1 ; Don't grab the section header ([issue##])
				{
					Loop,parse,A_loopfield,= ; Read chunks based on "="
					{
						If A_Index = 1 ; Grab the key
							line_name = %A_loopfield%
						If A_Index = 2 ; Grab/append the value (in case they used an "=" sign in their text
							line_contents := line_contents . A_loopfield
						If A_Index > 2  
							line_contents := line_contents . "=" . A_loopfield
					}
					; Put info in a dynamic (in this case, predictable) variable
					issue_%line_name% = %line_contents%
				}
			}
			; Add info to the listview
			FormatTime,issue_date,%issue_date%,MM/dd/yyyy
			LV_Add("",A_Loopfield,issue_date,issue_type,issue_priority,issue_status,issue_subject)
		}
	}
}

; If no rows have been added (i.e. nothing matched our search), print an error message
If !(LV_GetCount())
{
	num_issues := ini_getvalue(issues,"issues","number")
	If !(num_issues) ; If there aren't any issues (number is 0)
		noissues := get_language_string("noissuesmade",language)
	Else
		noissues := get_language_string("noissuesfound",language)
	LV_Add("",noissues)
}


; Adjust the column sizes to row or header text (whichever is bigger)
; adjust_cols:
Loop,6
{
	LV_ModifyCol(A_Index,"AutoHDR")
}

; Show the listview
Control,Show,,Syslistview321,%issue_win_title%

Return
}

; Old method for getting issue details. Why is it the old method?
; Performs ~1/2 of the speed of the loop method (in bulk, about 1/3 or less of the speed on smaller sets)
; Has much higher CPU usage than loop (at least 20-30x on my system during bulk test) 
; Gets CPU limited by AHKs threat timing "limiter" (it shares time with other running AHK threads). The command "Critical" can give it more CPU time, but even with some extra cycles it falls behind the loop very quickly.
; Not much surprise when you contrast how many lines of code are actually run by each (~30 lines per "getvalue"), loop is ~20

; issue_status := ini_getvalue(issues,"issue"A_Loopfield,"status")
; issue_type := ini_getvalue(issues,"issue"A_Loopfield,"type")
; issue_subject := ini_getvalue(issues,"issue"A_Loopfield,"subject")
; issue_priority := ini_getvalue(issues,"issue"A_Loopfield,"priority")
; issue_date := ini_getvalue(issues,"issue"A_Loopfield,"date")



